package org.example.dormsystem.controller;

import org.example.dormsystem.pojo.*;
import org.example.dormsystem.service.AdminService;
import org.example.dormsystem.service.StudentService;
import org.example.dormsystem.util.JwtUtil;
import org.example.dormsystem.util.Md5Util;
import org.example.dormsystem.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController{

    @Autowired
    AdminService adminService;

    @GetMapping("/register")
    Result register(String username,String password){//注册账户
        Admin admin = adminService.findAdminByUsername(username);
        if(admin==null){//判断账户是否存在
            if(adminService.register(username,password)){
                return Result.success();
            }
            else{
                return Result.error("未知原因导致注册失败");
            }
        }
        else{
            return Result.error("账户已存在，请修改");
        }
    }



    @GetMapping("/login")
    Result login(String username,String password){//登陆账户
        Admin admin = adminService.findAdminByUsername(username);
        if(admin!=null){
            if(admin.getPassword().equals(Md5Util.getMD5String(password))){//判断密码是否正确
                Map<String,Object> claims = new HashMap<>();
                claims.put("id",admin.getId());
                claims.put("username",admin.getUsername());
                claims.put("password",password);
                claims.put("contact",admin.getContact());
                String token = JwtUtil.genToken(claims);//将用户信息封装进token（生成JWT令牌）
                return Result.success(token);
            }
            else{
                return Result.error("密码错误！");
            }
        }
        else{
            return Result.error("账户输入错误或账户不存在");
        }
    }

    @GetMapping("/adminInfo")//管理员主页
    Result adminInfo(){
        Map<String,Object> map = ThreadLocalUtil.get();
        Admin admin = adminService.findAdminByUsername((String)map.get("username"));
        return Result.success(admin);
    }

//
//    @GetMapping("/searchStudentByDormitoryNumber")
//    Result searchStudent(String dormitoryNumber){
//
//        return null;
//    }

    @GetMapping("/check")
    Result check(Integer decide,Integer changeRecordId){//审核是否通过
        System.out.println(changeRecordId);
        if(decide==1){//通过
            adminService.passCheck(changeRecordId);
            return Result.success();
        }
        else if(decide==0){//不通过
            adminService.noPassCheck(changeRecordId);
            return Result.success();
        }
        return  Result.error("出现未知错误");
    }

    @GetMapping("/recordInfo")
    Result changeRecordInfo(){
        List<ChangeRecord> list = adminService.getRecordInfo();
        return Result.success(list);
    }

    @GetMapping("/submitMessage")//发送通知
    Result submitMessage(String message){
        adminService.submit(message);
        return Result.success();
    }
}
